<?php



/**

 * 登录控制器

 */

namespace Custom\Controller;

use Think\Controller;



class LoginController extends Controller {



    function __construct()

    {

        parent::__construct();

        $this->M_company = M('Company');

        $this->M_account = M('Account');

    }



    /*

     * 登录页面

     */

    public function index(){

        $this->assign('title',L('login'));

        if(session('c_id')) header("Location: ".__MODULE__."/Index");

        if(!IS_POST){

            die($this->display());

        }
        ///////////检查验证码////////////
        $this->check_verify($_POST['code']);
        
        $data['account_name'] = I('post.account_name');

        $data['password'] = MD5(I('post.password'));

        $info = $this->M_account->where($data)->find();
        if($info['s_id']){
            session('s_id',$info['s_id']);
        }

        $company_info = $this->M_company->find($info['c_id']);

        if($info){

            $newTime = time();

            $oldTime = $company_info['expire_time'];

            if ($newTime > $oldTime) {

                die($this->error('超过使用系统有效期！'));

            }



            session('c_id',$info['c_id']);

            session('account_id',$info['account_id']);

            session('is_son',$info['is_son']);

            session('company_name',$company_info['company_name']);

                $data = array(

                    'account_id' => $info['account_id'],

                    'login_time' => time(),

                );

                 $this->M_account->save($data);

                die($this->success(L('loginsuccess'),U('Index/index')));

        }else {

            die($this->error(L('loginmess')));

        }

    }



    /*

     * 退出登录

     */

    public function logout(){

        session('c_id',null);

        session('role_id',null);

        session('s_id',null);

        header("Location: ".__MODULE__."/Login");

    }

    /* 生成验证码 */
    public function verify()
    {
        $Verify = new \Think\Verify();
        $Verify->entry();
    }
    public function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        $res = $verify->check($code, $id);
        if(!$res){
            die($this->error('验证码错误'));
        }
    }

}